/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.panpf.sketch.sample.widget.page;

import me.panpf.sketch.SketchImageView;
import me.panpf.sketch.decode.ImageAttrs;
import me.panpf.sketch.display.TransitionImageDisplayer;
import me.panpf.sketch.process.MaskImageProcessor;
import me.panpf.sketch.process.RotateImageProcessor;
import me.panpf.sketch.process.RoundRectImageProcessor;
import me.panpf.sketch.request.CancelCause;
import me.panpf.sketch.request.DisplayListener;
import me.panpf.sketch.request.ErrorCause;
import me.panpf.sketch.request.ImageFrom;
import me.panpf.sketch.sample.AssetImage;
import me.panpf.sketch.sample.ResourceTable;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.utils.Color;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

import java.util.Locale;
import java.util.Optional;

/**
 * WrappedImageProcessorTestPageView
 */
public class WrappedImageProcessorTestPageView extends AbstractPageView {
    private Text mTextWrappedWidth;
    private Text mTextWrappedHeight;
    private Slider mSeekBarWrappedWidth;
    private Slider mSeekBarWrappedHeight;
    private SketchImageView sketchImageView;
    private int rotateProgress;
    private boolean isApplyDone;

    public WrappedImageProcessorTestPageView(Context context, String name) {
        super(context, name);
    }

    @Override
    public void initView() {
        super.setRootView(loadView());
        sketchImageView = (SketchImageView) getRootView().findComponentById(ResourceTable.Id_image_wrapped);
        Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(super.getContext());
        DisplayAttributes displayAttributes = display.get().getAttributes();
        sketchImageView.getOptions().setMaxSize(displayAttributes.width / 2, displayAttributes.height / 2);
        mTextWrappedWidth = (Text) getRootView().findComponentById(ResourceTable.Id_text_wrapped_width);
        mTextWrappedHeight = (Text) getRootView().findComponentById(ResourceTable.Id_text_wrapped_height);
        mSeekBarWrappedWidth = (Slider) getRootView().findComponentById(ResourceTable.Id_seekBar_wrapped_width);
        setSeekBarWidthParams();
        mSeekBarWrappedHeight = (Slider) getRootView().findComponentById(ResourceTable.Id_seekBar_wrapped_height);
        setSeekBarHeightParams();
        getRootView().findComponentById(ResourceTable.Id_button_wrapped).setClickedListener(component -> {
            if (!isApplyDone) {
                return;
            }
            rotateProgress += 45;
            apply();
        });
        rotateProgress = 45;
        isApplyDone = true;
        sketchImageView.setDisplayListener(new DisplayListener() {
            @Override
            public void onStarted() {
                isApplyDone = false;
            }

            @Override
            public void onCompleted(Element drawable, ImageFrom imageFrom, ImageAttrs imageAttrs) {
                isApplyDone = true;
            }

            @Override
            public void onError(ErrorCause cause) {
                isApplyDone = true;
            }

            @Override
            public void onCanceled(CancelCause cause) {
                isApplyDone = true;
            }
        });
        apply();
    }

    private void setSeekBarWidthParams(){
        mSeekBarWrappedWidth.setProgressValue(30);
        mSeekBarWrappedWidth.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i1, boolean b) {
                int progress = 20;
                if (i1 < progress) {
                    mSeekBarWrappedWidth.setProgressValue(progress);
                } else {
                    progress = i1;
                }
                mTextWrappedWidth.setText(String.format("%d/%d", progress * 10, 1000, Locale.ENGLISH));
            }

            @Override
            public void onTouchStart(Slider slider) {

            }

            @Override
            public void onTouchEnd(Slider slider) {
                apply();
            }
        });
    }

    private void setSeekBarHeightParams(){
        mSeekBarWrappedHeight.setProgressValue(45);
        mSeekBarWrappedHeight.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i1, boolean b) {
                int progress = 20;
                if (i1 < progress) {
                    mSeekBarWrappedHeight.setProgressValue(progress);
                } else {
                    progress = i1;
                }
                mTextWrappedHeight.setText(String.format("%d/%d", progress * 10, 1000, Locale.ENGLISH));
            }

            @Override
            public void onTouchStart(Slider slider) {

            }

            @Override
            public void onTouchEnd(Slider slider) {
                apply();
            }
        });
    }

    private Component loadView() {
        ComponentContainer layout = (ComponentContainer) LayoutScatter.getInstance(super.getContext()).parse
                (ResourceTable.Layout_wrapped_page_view, null, false);

        return layout;
    }

    private void apply() {
        if (!isApplyDone) {
            return;
        }
        isApplyDone = false;
        int alpha = mSeekBarWrappedHeight.getProgress() * 255 / 100;
        int maskColor = Color.argb(alpha, 0, 0, 0);
        float roundRectRadius = mSeekBarWrappedWidth.getProgress();
        RoundRectImageProcessor roundRectImageProcessor = new RoundRectImageProcessor(roundRectRadius);
        RotateImageProcessor rotateImageProcessor = new RotateImageProcessor(rotateProgress, roundRectImageProcessor);
        sketchImageView.getOptions().setProcessor(new MaskImageProcessor(maskColor, rotateImageProcessor));
        sketchImageView.displayResourceImage(ResourceTable.Media_mei_nv);
    }
}
